/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示通过BTIM提供PCLK级别精准的延时函数
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：None
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_btim.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    uint16_t pclk2delay = 100;

    // 初始化系统时钟
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI4MHZ);

    // 从PA02输出PCLK
    HAL_SysCtrl_ClkOut_PCLK_PA02();

    // 初始化板载LED
    Bsp_Led_Init();

    // 使能BTIM时钟
    HAL_SYSCTRL_BTIM_CLK_ENABLE();

    while (1)
    {
        // 延时100个PCLK周期   1/4000000 * 100 = 25us
        BSP_LED_SETHIGH();
        HAL_BTIM_DelayPclk(SYM_BTIM1, pclk2delay - 1);                // 该函数最少占用34个PCLK时间，延时时间最小为34PCLK周期
        BSP_LED_SETLOW();
        HAL_BTIM_DelayPclk(SYM_BTIM1, pclk2delay - 1);

        BSP_LED_SETHIGH();
        HAL_BTIM_DelayPclk(SYM_BTIM1, pclk2delay - 1);
        BSP_LED_SETLOW();
        HAL_BTIM_DelayPclk(SYM_BTIM1, pclk2delay - 1);
    }
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
